package com.example.weather.Other

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.net.ConnectivityManager
import android.os.Build
import android.widget.Toast
import androidx.annotation.RequiresApi

/**
 * @author Y7993
 */
//广播 网络的文件
// 此类用于监听网络状态的变化
class NetworkStateBroadcast : BroadcastReceiver() {
    @RequiresApi(api = Build.VERSION_CODES.M)  // 当接收到网络状态变化的广播时调用此方法
    override fun onReceive(context: Context, intent: Intent?) {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val network = cm.activeNetwork
        if (network == null) {
            Toast.makeText(context, "当前网络无连接", Toast.LENGTH_SHORT).show()
            println("当前网络无连接")
        } else {
            Toast.makeText(context, "当前网络恢复连接", Toast.LENGTH_SHORT).show()
            println("当前网络恢复连接")
        }
    }
}

